package com.example.trueordare.activity;

import androidx.annotation.NonNull;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.example.trueordare.R;
import com.example.trueordare.model.Question;
import com.example.trueordare.tools.QuestionTools;

public class AnswerListActivity extends AppCompatActivity {
    private RecyclerView mAnswers; //可滚动列表控件
    private Button mShare;         //分享按钮
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_answer_list);
        mAnswers = findViewById(R.id.view_answers);    //获取RecyclerView对象
        mAnswers.setLayoutManager(new LinearLayoutManager(
                AnswerListActivity.this));     //设置布局管理
        mShare = findViewById(R.id.button_share);
        mShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                QuestionTools.getInstance().sendMessage(AnswerListActivity.this);
            }
        });
        //指定适配器
        mAnswers.setAdapter(new AnswerAdapter());

    }

    //适配器
    private class AnswerAdapter extends RecyclerView.Adapter<AnswerViewHolder>{

        private Question[] mQuestions;   //存放问题和答案的数组

        public AnswerAdapter(){
            //获取QuestionTools中已存储的问题和答案的数组
            mQuestions = QuestionTools.getInstance().getQuestions();
        }
        @NonNull
        @Override
        public AnswerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            //加载列表项的视图
            LayoutInflater inflater = LayoutInflater.from(parent.getContext());
            //创建列表项的布局
            View view = inflater.inflate(R.layout.answer_item, parent, false);

            return new AnswerViewHolder(view);//**返回给ViewHolder
        }

        @Override
        public void onBindViewHolder(@NonNull AnswerViewHolder holder, int position) {
            Question question = mQuestions[position];
            holder.bindData(question,position + 1);
        }

        @Override
        public int getItemCount() {
            return mQuestions.length;
        }
    }

    //列表项View的固定器Holder
    private class AnswerViewHolder extends RecyclerView.ViewHolder{
        private TextView mTextIndex;    //序号显示
        private TextView mTextQuestion; //问题显示
        private TextView mTextAnswer;   //答案显示
        public AnswerViewHolder(@NonNull View itemView) {
            super(itemView);
            //加载布局内部控件 即三个TextView
            mTextIndex = itemView.findViewById(R.id.question_index);
            mTextQuestion = itemView.findViewById(R.id.text_view_question);
            mTextAnswer = itemView.findViewById(R.id.text_view_answer);

        }

        //由AnswerAdapter的onBindViewHolder()调用 进行数据展示

        @SuppressLint("ResourceAsColor")
        public void bindData(Question question, int position){
            mTextIndex.setText(String.valueOf(position + "、"));
            mTextQuestion.setText(question.getQuestionId());
            mTextAnswer.setText(question.getAnswer());
            if(position % 2 == 1){
                mTextIndex.setTextColor(R.color.question_text_color);
                mTextQuestion.setTextColor(R.color.question_text_color);
            }
        }


    }

    public static Intent newIntent(Context context){
        return new Intent(context, AnswerListActivity.class);
    }
}